****************************************************************************************************************************
Date: 29JAN 2015

Purpose: Add parental use of antidepressants

****************************************************************************************************************************;



*Antidepressiva findes under ATC koden N06A. ;

libname in 'F:\Rawdata\702727\data201006';

*Laver en macro hvor data indlæses for de tre medicin typer af interesse. I samme macro laves en række generelle ændringer.;

%macro antidep_medi(start,end);
%do i=&start %to &end;

Data antidep_medi&i;   *Data defineres;
set in.lmdb&i(KEEP=PNR ATC EKSD APK volume RECU);
	Year = year(EKSD);
	rename volume=DDD;		*OBS: DDD står for Defined Daily Doses;
	ATC4=substr(ATC, 1, 4);
Proc sort;
by pnr year;
run;

Data antidep_medi&i;
set antidep_medi&i;
if ATC4='N06A' then output antidep_medi&i;
run;


*Finder her det totale antal doser ved hvert medicin køb. (beregnes som: antal pakker*indhold);
Data antidep_medi&i;
set antidep_medi&i;
By pnr;

TotalDDD= APK * DDD; 

IF first.pnr then akkDDD= TotalDDD; *Dagsdoser akkumuleres;
ELSE akkDDD + TotalDDD;

*Gemmer kun den sidste observation for hver person. Herved indgår hver person med deres årlige akkumulerede medicin.
Senere merges den for hvert år;

IF last.PNR;
KEEP PNR YEAR ATC ATC4 akkDDD RECU;
run;


%end;
%mend;
%antidep_medi(1995,2011);
run;


data antidep_med;
set antidep_medi1995-antidep_medi2011;
run;

proc datasets;
delete antidep_medi1995-antidep_medi2011;
quit;
 
proc sort data=antidep_med;
by pnr year;
run;

data antidep_med;
set antidep_med (keep=pnr year atc akkddd);
antidep=1;
run;

*Gemmer kun pnr, år og indikator for berørt med antidepressiva;
data antidep_med1;
set antidep_med;
keep pnr year antidep;
run;

****************************************************************
Omdøber variable så de kan matches med forældre i mit grunddata
****************************************************************;

data antidep_mor;
set antidep_med1;

rename pnr=pnrm;
rename year=year_mom;
rename antidep=antidep_mom;
run;

data antidep_mor;
set antidep_mor;
label pnrm="Personnummer - mor";
run;

data antidep_far;
set antidep_med1;

rename pnr=pnrf;
rename year=year_dad;
rename antidep=antidep_dad;
run;
data antidep_far;
set antidep_far;
label pnrf="Personnummer - far";
run;

proc sort data=antidep_mor;
by pnrm;
run;
proc sort data=antidep_far;
by pnrf;
run;




*****************************************************************************
Indhenter data om barnets fødselsdato

Må tilføje antidepressiva brug for hvert enkelt af moderens/faderen børn
	- Opdele child data i mors forskellige børn
	- Tilføjer antidep til det enkelte barn
*****************************************************************************;
libname in2 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in2.nicu_parentalhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;

*Opdeler sample i moderens børn;
proc sort data=child;
by pnrm birthyear;
run;

data child;
set child;
d1=1;
by pnrm;
if first.pnrm then d2=d1;
else d2+d1;
run;

proc freq data=child;
table d2;
run;


*Opdeler sample i moderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child ;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


***************************************************************************
Merger nu antidep_med på hver enkel sample med hvert enkelt barn.

- På den måde kan jeg få et mål for forældres med brug på forskellige aldre.
	Uanset birthorder mm. 
****************************************************************;

%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;
data antidepmor_barn&i (drop=d1 d2);
merge barn&i (in=a) antidep_mor;
by pnrm;
if a;
run;

data antidepmor_barn&i;
set antidepmor_barn&i;

*Har mor på noget tidspunt efter fødsel modtaget antidepressiva;
M_antidep_any=0;
if year_mom>=birthyear and antidep_mom=1 then M_antidep_any=1;

*Har mor ved en specifik alder modtaget antidepressiva;
%macro time(start,slut);
%do j=&start %to &slut;

M_antidep&j=.;
if year_mom-birthyear=&j and antidep_mom=1 then M_antidep&j=1;
/*if birthyear+&j>=1995 and antidep_mom=. then M_antidep&j=0;*/

%end; 
%mend;
%time(2,18);
run;

proc means data=antidepmor_barn&i NWAY NOPRINT;
class pnrm;
var 
M_antidep_any M_antidep2 M_antidep3 M_antidep4 M_antidep5 M_antidep6 M_antidep7 M_antidep8 M_antidep9 
M_antidep10 M_antidep11 M_antidep12 M_antidep13 M_antidep14 M_antidep15 M_antidep16 M_antidep17 M_antidep18;
Output Out=temp_antidepmor_barn&i min=;
run;


*Merger nu de dannede indkomstmål for år 0-10 på hvert barn;
proc sort data=temp_antidepmor_barn&i (drop=_TYPE_ _FREQ_);
by pnrm;
run;

proc sort data=barn&i (drop= d1 d2);
by pnrm ;
run;

data Mantidep_barn&i;
merge barn&i (in=a) temp_antidepmor_barn&i;
by pnrm;
if a;
run;

%end; 
%mend;
%barn(1,8);
run;


*Kan nu sætte det 8 børnedtasæt sammen igen;
data Mantidep;
set Mantidep_barn1-Mantidep_barn8;
run;

proc sort data=Mantidep;
by pnrm birthyear;
run;


data Mantidep;
set Mantidep;

%macro timetwo(start,slut);
%do k=&start %to &slut;

if birthyear+&k>=1995 and birthyear+&k<=2011 and M_antidep&k=. then M_antidep&k=0;

%end; 
%mend;
%timetwo(2,18);
run;

proc datasets;
delete child;
quit; 
proc datasets;
delete Mantidep_barn1-Mantidep_barn8;
quit; 
proc datasets;
delete antidepmor_barn1-antidepmor_barn8;
quit;
proc datasets;
delete temp_antidepmor_barn1-temp_antidepmor_barn8;
quit;
proc datasets;
delete barn1-barn8;
quit; 



*****************************************************************************
Gør nu tilsvarende for far
*****************************************************************************;
libname in3 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in3.nicu_parentalhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;

*Opdeler sample i faderens børn;
proc sort data=child;
by pnrf birthyear;
run;

data child;
set child;
d1=1;
by pnrf;
if first.pnrf then d2=d1;
else d2+d1;
run;


*Opdeler sample i faderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child ;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


***************************************************************************
Merger nu antidep_med på hver enkel sample med hvert enkelt barn.

- På den måde kan jeg få et mål for forældres med brug på forskellige aldre.
	Uanset birthorder mm. 
****************************************************************;

%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;
data antidepfar_barn&i (drop=d1 d2);
merge barn&i (in=a) antidep_far;
by pnrf;
if a;
run;

data antidepfar_barn&i;
set antidepfar_barn&i;

*Har mor på noget tidspunt efter fødsel modtaget antidepressiva;
F_antidep_any=0;
if year_dad>=birthyear and antidep_dad=1 then F_antidep_any=1;

*Har mor ved en specifik alder modtaget antidepressiva;
%macro time(start,slut);
%do j=&start %to &slut;

F_antidep&j=.;
if year_dad-birthyear=&j and antidep_dad=1 then F_antidep&j=1;


%end; 
%mend;
%time(2,18);
run;

proc means data=antidepfar_barn&i NWAY NOPRINT;
class pnrf;
var 
F_antidep_any F_antidep2 F_antidep3 F_antidep4 F_antidep5 F_antidep6 F_antidep7 F_antidep8 F_antidep9 
F_antidep10 F_antidep11 F_antidep12 F_antidep13 F_antidep14 F_antidep15 F_antidep16 F_antidep17 F_antidep18;
Output Out=temp_antidepfar_barn&i min=;
run;


*Merger nu de dannede indkomstmål for år 0-10 på hvert barn;
proc sort data=temp_antidepfar_barn&i (drop=_TYPE_ _FREQ_);
by pnrf;
run;

proc sort data=barn&i (drop= d1 d2);
by pnrf ;
run;

data Fantidep_barn&i;
merge barn&i (in=a) temp_antidepfar_barn&i;
by pnrf;
if a;
run;

%end; 
%mend;
%barn(1,8);
run;


*Kan nu sætte det 8 børnedtasæt sammen igen;
data Fantidep;
set Fantidep_barn1-Fantidep_barn8;
run;

proc sort data=Fantidep;
by pnrf birthyear;
run;


data Fantidep;
set Fantidep;

%macro timetwo(start,slut);
%do k=&start %to &slut;

if birthyear+&k>=1995 and birthyear+&k<=2011 and F_antidep&k=. then F_antidep&k=0;

%end; 
%mend;
%timetwo(2,18);
run;

proc datasets;
delete child;
quit; 
proc datasets;
delete Fantidep_barn1-Fantidep_barn8;
quit; 
proc datasets;
delete antidepfar_barn1-antidepfar_barn8;
quit;
proc datasets;
delete temp_antidepfar_barn1-temp_antidepfar_barn8;
quit;
proc datasets;
delete barn1-barn8;
quit; 



**************************************************************************
Samler nu i ét datasæt - merge på mit fulde datasæt
**************************************************************************;
libname in4 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in4.nicu_parentalhealth;
run;
proc sort data=Mantidep;
by pnr;
run;

proc sort data=Fantidep;
by pnr;
run;

proc sort data= child;
by pnr;
run;

*******************************************
Kan nu merge dette på mit fulde datasæt
*******************************************;

*mor;
data sample_antidep;
merge child (in=a) Mantidep;
by pnr;
if a;
run;
*far;
data sample_antidep;
merge sample_antidep (in=b) Fantidep;
by pnr;
if b;
run;



proc means data=sample_antidep;
var 
M_antidep_any M_antidep2 M_antidep3 M_antidep4 M_antidep5 M_antidep6 M_antidep7 M_antidep8 M_antidep9 
M_antidep10 M_antidep11 M_antidep12 M_antidep13 M_antidep14 M_antidep15 M_antidep16 M_antidep17 M_antidep18
F_antidep_any F_antidep2 F_antidep3 F_antidep4 F_antidep5 F_antidep6 F_antidep7 F_antidep8 F_antidep9 
F_antidep10 F_antidep11 F_antidep12 F_antidep13 F_antidep14 F_antidep15 F_antidep16 F_antidep17 F_antidep18;
run;


data sample_antidep;
set sample_antidep;

%macro NR(start,slut);
%do i=&start %to &slut;

if M_intsup&i>260 then M_intsup&i=260;
if F_intsup&i>260 then F_intsup&i=260;

%end; 
%mend;
%NR(0,10);
run;


*Ændre rækkefølgen af variablene;

data sample;

retain 
pnr pnrm pnrf FODTDATO birthyear birthyear_fb multiple birth_order family_size
male apgar bw ga 
dead24h dead28d dead1y

mom_birthdate mom_birthyear dad_birthdate dad_birthyear mom_age dad_age mom_age_fb dad_age_fb 
mom_elen dad_elen mom_elen_impute dad_elen_impute M_education_mis F_education_mis mom_Basic mom_hsvoc mom_college dad_Basic dad_hsvoc dad_college
M_employed F_employed M_employed_mis F_employed_mis M_income_mis F_income_mis M_income F_income total_income  
mom_immigrant dad_immigrant dad_missing
married_birth cohabiting_birth married_cohab_birth married_birth_mis married_cohab_birth_mis

birth_hospital birth_municip birth_county birth_region

karakter1 Dansk_m_prove1 Mat_s_prove1 hs_enrollment
alder_crime crime15 crime16 crime17 crime18 crime19 crime20 crime21
fatherhood20 motherhood20 parenthood20

PSYCH_diag MR_diag BE_diag CP_diag EP_diag EYE_diag EAR_diag ASTHMA_diag CONGENITAL_diag F80_83_diag ADHD_diag

divorce_year0-divorce_year10
leave
M_income0-M_income10 
F_income0-F_income10
M_extsup0-M_extsup10 M_intsup0-M_intsup10
F_extsup0-F_extsup10 F_intsup0-F_intsup10

CIRCdiag_mom_before CIRCdiag_mom_after CIRCdiag_dad_before CIRCdiag_dad_after
RESPdiag_mom_before RESPdiag_mom_after RESPdiag_dad_before RESPdiag_dad_after
PSYCHdiag_mom_before PSYCHdiag_mom_after PSYCHdiag_dad_before PSYCHdiag_dad_after

M_antidep_any M_antidep2-M_antidep18
F_antidep_any F_antidep2-F_antidep18
 
;

set sample_antidep (drop=smoking half_sib Hospital_contact Hospital_contact_mom);
run;


data test;
set sample;

if birthyear<1982 then delete;
if birthyear>1993 then delete;
if bw=. then delete;
if ga=. then delete;

run;


proc freq data=test;
table leave;
run;


proc means data=test;
var 
PSYCH_diag MR_diag BE_diag CP_diag EP_diag EYE_diag EAR_diag ASTHMA_diag CONGENITAL_diag F80_83_diag ADHD_diag
divorce_year0-divorce_year10
leave
M_income0-M_income10 
F_income0-F_income10
M_extsup0-M_extsup10 M_intsup0-M_intsup10
F_extsup0-F_extsup10 F_intsup0-F_intsup10

CIRCdiag_mom_before CIRCdiag_mom_after CIRCdiag_dad_before CIRCdiag_dad_after
RESPdiag_mom_before RESPdiag_mom_after RESPdiag_dad_before RESPdiag_dad_after
PSYCHdiag_mom_before PSYCHdiag_mom_after PSYCHdiag_dad_before PSYCHdiag_dad_after

M_antidep_any M_antidep2-M_antidep18
F_antidep_any F_antidep2-F_antidep18;
run;





*************
Gemmer data
*************;

*SAS datasæt;
libname out 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data out.nicu_all;
set sample;
run;



*STATA datasæt;
proc export data=sample outfile="K:\Data\Workdata\702727\xru\NICU project\Work camp - aug_sept\Data\nicu_data_290115.dta" dbms=stata replace;
run;
